#define _CRT_SECURE_NO_WARNINGS

#include "game.h"

void color(int c)
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);
}

void ClBoard(char board[ROWS][COLS], int row, int col)
{
	int i;
	int j;
	for (i = 4; i < row + 4; i++)
	{
		for (j = 4; j < col + 4; j++)
		{
			board[i][j] = ' ';
		}
	}
	for (i = 4; i < ROWS; i++)
	{
		for (j = 4; j < COLS; j++)
		{
			if((i < 4 && (j < 4 || j > COLS - 4)) || (i > ROWS - 4 && (j < 4 || j > COLS - 4)))
			board[i][j] = '?';
		}
	}
}

void PrBoard(char board[ROWS][COLS], int row, int col)
{
	int i;
	int j;
	printf("   ");
	for (i = 4; i < col + 4; i++)
	{
		printf("%-2d ", i - 3);
		if (i < col + 3)
			printf(" ");
	}
	printf("\n");
	for (i = 4; i < row + 4; i++)
	{
		printf("%2d", i - 3);
		for (j = 4; j < col + 4; j++)
		{
			if (board[i][j] == '#')
				color(9);
			if (board[i][j] == '*')
				color(10);
			printf(" %c ", board[i][j]);
			color(7);
			if (j < col + 3)
				printf("|");
		}
		printf("\n");
		if (i < row + 3)
		{
			printf("  ");
			for (j = 4; j < col + 4; j++)
			{
				printf("---");
				if (j < col + 3)
					printf("|");
			}
		}
		printf("\n");
	}
}

//char IsWin(char board[ROW][COL], int row, int col)
//{
//	int i, j, sign = 0;
//	for (i = 0; i < row; i++)
//	{
//		for (j = 0; j < col; j++)
//		{
//			if (board[i][j] != ' ')
//				sign++;
//			if (board[i][j] == board[i][j + 1] && board[i][j + 1] == board[i][j + 2] && board[i][j] != ' ')
//				return board[i][j];
//			if (board[i][j] == board[i + 1][j] && board[i + 1][j] == board[i + 2][j] && board[i][j] != ' ')
//				return board[i][j];
//			if (board[i][j] == board[i + 1][j + 1] && board[i + 1][j + 1] == board[i + 2][j + 1] && board[i][j] != ' ')
//				return board[i][j];
//			if (board[i][j] == board[i - 1][j - 1] && board[i - 1][j - 1] == board[i - 2][j - 1] && board[i][j] != ' ')
//				return board[i][j];
//		}
//	}
//	if (sign == row * col)
//		return 'Q';
//	return 'C';
//}

char IsWin(char board[ROWS][COLS], int row, int col)
{
	int i, j, k, sign = 0;
	for (i = 4; i < row + 4; i++)
	{
		for (j = 0; j < col + 4; j++)
		{
			int ret1 = 1, ret2 = 1, ret3 = 1, ret4 = 1;
			if (board[i][j] != ' ')
				sign++;
			for (k = 1; k < RULER; k++)
			{
				if (board[i][j] == board[i][j + k] && board[i][j] != ' ')
					ret1++;
				if (board[i][j] == board[i + k][j] && board[i][j] != ' ')
					ret2++;
				if (board[i][j] == board[i + k][j + k] && board[i][j] != ' ')
					ret3++;
				if (board[i][j] == board[i - k][j + k] && board[i][j] != ' ')
					ret4++;
			}
			if (ret1 == RULER || ret2 == RULER || ret3 == RULER || ret4 == RULER)
				return board[i][j];
		}
	}
	if (sign == row * col)
		return 'Q';
	return 'C';
}